<?php
/**
 * Controller profilu uzivatela
 * @author Tomas Srna
 * @package bakalarka
 */

class ProfileController extends Controller
{
	protected $resource = 'index';
	/**
	 * Akcia index
	 */
	function index()
	{
		$t = Template::instance();
		
		$t->title = L::selected()->editProfile;
		
		$model = Auth::instance()->getUser();
		
		if(getenv('REQUEST_METHOD') == 'POST')
		{
			$f = new ProfileForm($t, $_POST);
			
			if($f->validate())
			{
				if(array_key_exists('username', $_POST))
					unset($_POST['username']);
				if(array_key_exists('group', $_POST))
					unset($_POST['group']);
				
				$model->set($_POST);
				
				$model->save();
				
				Logger::log('L::objectChanged_s;'.
						json_encode((string)$model), 'info');
				$this->autoredir();
			}
			else
				$t->error = L::selected()->invalidForm;
		}
		else
		{
			$f = new ProfileForm($t, $model);
		}
		
		$fw = new FormWriter($f);
		$t->fw = $fw;
		
		$t->render('form');
	}
}
